﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

using System.Text;
using HK2009.Common;
using HK2009.BusinessFacade;
using HK2009.BusinessFacadeHandler;
using HK2009.EntityQuery;
using System.Data.SqlClient;
using System.Threading;
using System.Globalization;
public partial class MasterPage_MasterPage : System.Web.UI.MasterPage
{
    IRolePurviewHandler userManage = new RolePurviewHandler();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["LoginID"] == null || Session["UserID"] == null)
        {
            Response.Redirect("../Login.aspx");
        }
        if (!IsPostBack)
        {
            if (Request.Cookies["Language"] == null)
            {
                if (Request.UserLanguages.Length > 0)
                {
                    if (Request.UserLanguages[0].ToLower().Equals("zh-hk"))
                    {
                        Thread.CurrentThread.CurrentUICulture = new CultureInfo(Config.Cht.ToLower());
                    }
                    else if (Request.UserLanguages[0].ToLower().Contains("en"))
                    {
                        Thread.CurrentThread.CurrentUICulture = new CultureInfo(Config.En.ToLower());
                    }
                    else
                    {
                        Thread.CurrentThread.CurrentUICulture = new CultureInfo(Config.Chs.ToLower());
                    }
                }
                else
                {
                    Thread.CurrentThread.CurrentUICulture = new CultureInfo(Config.Chs.ToLower());
                }
            }
            else
            {
                string Language = Request.Cookies["Language"].Value;
                if (Language.Length > 0)
                {
                    Thread.CurrentThread.CurrentUICulture = new CultureInfo(Language);
                }
            }

            dataBind();
        }
    }
    protected void lbtnTW_Click(object sender, EventArgs e)
    {
        HttpCookie hc = new HttpCookie("Language", "zh-hk");
        Response.SetCookie(hc);
        Response.Redirect(Request.Url.ToString());

    }
    protected void lbtnCHN_Click(object sender, EventArgs e)
    {
        HttpCookie hc = new HttpCookie("Language", "zh-cn");
        Response.SetCookie(hc);
        Response.Redirect(Request.Url.ToString());
    }
    protected void lbtnEnglish_Click(object sender, EventArgs e)
    {
        HttpCookie hc = new HttpCookie("Language", "en");
        Response.SetCookie(hc);
        Response.Redirect(Request.Url.ToString());

    }

    private void dataBind()
    {

        if (Session["LoginID"] != null)
        {
            lblLoginID.Text = Session["LoginID"].ToString();

            StringBuilder sb = new StringBuilder();

            UsersQuery query = userManage.GetUsers(int.Parse(Session["UserID"].ToString()));
            if (query != null)
            {
                string roleIDs = query.Roles;
                if (roleIDs != string.Empty)
                {
                    DataBase db = new DataBase();
                    string strSql = "select * from Roles where RoleID in(" + roleIDs + ")";
                    DataTable dtRoles = db.RunSql(strSql);
                    if (dtRoles != null && dtRoles.Rows.Count > 0)
                    {
                        for (int i = 0; i < dtRoles.Rows.Count; i++)
                        {
                            sb.Append(dtRoles.Rows[i]["RolePurview"].ToString() + ",");
                        }
                    }

                    string permission = sb.ToString();
                    if (permission.Length > 0)
                    {
                        permission = permission.Substring(0, permission.Length - 1);


                        CheckPermission(permission);//判断所有子菜单

                        //用户管理
                        if (permission.Contains("1006") || permission.Contains("1007") || permission.Contains("1010"))
                        {
                            divUserManage.Visible = true;
                        }
                        else
                        {
                            divUserManage.Visible = false;
                        }

                        //权限管理
                        if (permission.Contains("1001") || permission.Contains("1002") || permission.Contains("1003"))
                        {
                            divPurviewManage.Visible = true;
                        }
                        else
                        {
                            divPurviewManage.Visible = false;
                        }

                        //系统管理
                        if (permission.Contains("1011") || permission.Contains("1015") || permission.Contains("1019") || permission.Contains("1023") || permission.Contains("1027") || permission.Contains("1034") || permission.Contains("1079"))
                        {
                            divSyManage.Visible = true;
                        }
                        else
                        {
                            divSyManage.Visible = false;
                        }

                        //司机车辆管理
                        if (permission.Contains("1038") || permission.Contains("1042"))
                        {
                            divTruckManage.Visible = true;
                        }
                        else
                        {
                            divTruckManage.Visible = false;
                        }
                        //Booking管理
                        if (permission.Contains("1046") || permission.Contains("1048") || permission.Contains("1050"))
                        {
                            divBookingManage.Visible = true;
                        }
                        else
                        {
                            divBookingManage.Visible = false;
                        }

                        //托运单管理
                        if (permission.Contains("1053") || permission.Contains("1059"))
                        {
                            divBookingNoteManage.Visible = true;
                        }
                        else
                        {
                            divBookingNoteManage.Visible = false;
                        }

                        //Job管理
                        if (permission.Contains("1060") || permission.Contains("1061") || permission.Contains("1062") || permission.Contains("1071") || permission.Contains("1077") || permission.Contains("1088") || permission.Contains("1089"))
                        {
                            divJobManage.Visible = true;
                        }
                        else
                        {
                            divJobManage.Visible = false;
                        }

                        //车辆动态管理
                        if (permission.Contains("1066") || permission.Contains("1069"))
                        {
                            divTrackingManage.Visible = true;
                        }
                        else
                        {
                            divTrackingManage.Visible = false;
                        }

                        //报表管理
                        if (permission.Contains("1074") || permission.Contains("1075") || permission.Contains("1080") || permission.Contains("1081") || permission.Contains("1082") || permission.Contains("1084") || permission.Contains("1085") || permission.Contains("1086") || permission.Contains("1087"))
                        {
                            divReportManage.Visible = true;
                        }
                        else
                        {
                            divReportManage.Visible = false;
                        }
                    }

                }
            }
        }

    }

    private void CheckPermission(string OperationIDs)
    {
        foreach (Control con in form1.Controls)
        {
            string conID = con.ID;
            if (conID != null && conID.ToLower().StartsWith("auth"))
            {
                string permissionID = conID.Substring(4);
                if (!OperationIDs.Contains(permissionID))
                {
                    con.Visible = false;
                }               
            }
        }
    }

}
